home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / drivers / block / falhd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-19  |  19.3 KB  |  806 lines

  1. /*
  2.  *  linux/drivers/block/falhd.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  *
  6.  */
  7.  
  8. /*
  9.  * This is the low-level hd interrupt support. It traverses the
  10.  * request-list, using interrupts to jump between functions. As
  11.  * all the functions are called within interrupts, we may not
  12.  * sleep. Special care is recommended.
  13.  * 
  14.  *  modified by Drew Eckhardt to check nr of hd's from the CMOS.
  15.  *
  16.  *  Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
  17.  *  in the early extended-partition checks and added DM partitions
  18.  *
  19.  *
  20.  *  Modified 1994 for ATARI IDE controller support by Bjoern Brauel
  21.  */
  22.  
  23. #include <linux/errno.h>
  24. #include <linux/signal.h>
  25. #include <linux/sched.h>
  26. #include <linux/timer.h>
  27. #include <linux/fs.h>
  28. #include <linux/kernel.h>
  29. #include <linux/genhd.h>
  30.  
  31. #include <linux/bootinfo.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/atarihw.h>
  34. #include <linux/atarihdreg.h>
  35. #include <linux/atariints.h>
  36.  
  37. #include <asm/system.h>
  38. #include <asm/io.h>
  39. #include <asm/segment.h>
  40.  
  41. #define MAJOR_NR HD_MAJOR
  42. #include "blk.h"
  43.  
  44. static int revalidate_hddisk(int, int);
  45.  
  46. #define MAX_ERRORS     16    /* Max read/write errors/sector */
  47. #define RESET_FREQ      8    /* Reset controller every 8th retry */
  48. #define RECAL_FREQ      4    /* Recalibrate every 4th retry */
  49. #define MAX_HD        2
  50.  
  51. static void recal_intr(void);
  52. static void bad_rw_intr(void);
  53.  
  54. static char recalibrate[ MAX_HD ] = { 0, };
  55. static int access_count[MAX_HD] = {0, };
  56. static char busy[MAX_HD] = {0, };
  57. static struct wait_queue * busy_wait = NULL;
  58.  
  59. static int reset = 0;
  60. static int hd_error = 0;
  61.  
  62. /*
  63.  *  This struct defines the HD's and their types.
  64.  */
  65. struct hd_i_struct {
  66.     unsigned int head,sect,cyl,wpcom,lzone,ctl;
  67.     };
  68. struct hd_i_struct hd_info[] = { {0,0,0,0,0,0},{0,0,0,0,0,0} };
  69. static int NR_HD = 0;
  70.  
  71. static struct hd_struct hd[MAX_HD<<6]={{0,0},};
  72. static int hd_sizes[MAX_HD<<6] = {0, };
  73. static int hd_blocksizes[MAX_HD<<6] = {0, };
  74.  
  75. #define HD_ID_RETRIES  500000
  76.  
  77. #define port_read(port, buf, nr) \
  78. __asm__ __volatile__ \
  79.        ("movel %0,a0; \
  80.          movel %1,a1; \
  81.          movel %2,d6; \
  82.          1:movew a0@,a1@+; \
  83.          movew a0@,a1@+; \
  84.          movew a0@,a1@+; \
  85.          movew a0@,a1@+; \
  86.          movew a0@,a1@+; \
  87.          movew a0@,a1@+; \
  88.          movew a0@,a1@+; \
  89.          movew a0@,a1@+; \
  90.          movew a0@,a1@+; \
  91.          movew a0@,a1@+; \
  92.          movew a0@,a1@+; \
  93.          movew a0@,a1@+; \
  94.          movew a0@,a1@+; \
  95.          movew a0@,a1@+; \
  96.          movew a0@,a1@+; \
  97.          movew a0@,a1@+; \
  98.          dbra d6,1b" : : "i" (port), "g" (buf), "g" (nr) : "a0", "a1", "d6");
  99.  
  100. #define port_write(port, buf, nr) \
  101. __asm__ __volatile__ \
  102.        ("movel %0,a0; \
  103.          movel %1,a1; \
  104.          movel %2,d6; \
  105.        1:movew a1@+,a0@; \
  106.          movew a1@+,a0@; \
  107.          movew a1@+,a0@; \
  108.          movew a1@+,a0@; \
  109.          movew a1@+,a0@; \
  110.          movew a1@+,a0@; \
  111.          movew a1@+,a0@; \
  112.          movew a1@+,a0@; \
  113.          movew a1@+,a0@; \
  114.          movew a1@+,a0@; \
  115.          movew a1@+,a0@; \
  116.          movew a1@+,a0@; \
  117.          movew a1@+,a0@; \
  118.          movew a1@+,a0@; \
  119.          movew a1@+,a0@; \
  120.          movew a1@+,a0@; \
  121.          dbra d6,1b" : : "i" (port), "g" (buf), "g" (nr) : "a0", "a1", "d6");
  122.  
  123. void hd_setup(char *str, int *ints)
  124. {
  125.     int hdind = 0;
  126.  
  127.     if (ints[0] != 3)
  128.         return;
  129.     if (hd_info[0].head != 0)
  130.         hdind=1;
  131.     hd_info[hdind].head = ints[2];
  132.     hd_info[hdind].sect = ints[3];
  133.     hd_info[hdind].cyl = ints[1];
  134.     hd_info[hdind].wpcom = 0;
  135.     hd_info[hdind].lzone = ints[1];
  136.     hd_info[hdind].ctl = 0;
  137. }
  138.  
  139. static int win_result(void)
  140. {
  141.     int i=inb_p(HD_STATUS);
  142.  
  143.     if ((i & (BUSY_STAT | READY_STAT | WRERR_STAT | SEEK_STAT | ERR_STAT))
  144.         == (READY_STAT | SEEK_STAT)) {
  145.             hd_error = 0;
  146.         return 0; /* ok */
  147.     }
  148.     printk("HD: win_result: status = 0x%02x\n",i);
  149.     if (i&1) {
  150.         hd_error = inb(HD_ERROR);
  151.         printk("HD: win_result: error = 0x%02x\n",hd_error);
  152.     }    
  153.     return 1;
  154. }
  155.  
  156. static int controller_busy(void);
  157. static int status_ok(void);
  158.  
  159. static int controller_ready(unsigned int drive, unsigned int head)
  160. {
  161.     int retry = 100;
  162.  
  163.     do {
  164.         if (controller_busy() & BUSY_STAT)
  165.             return 0;
  166.         outb_p(0xA0 | (drive<<4) | head, HD_CURRENT);
  167.         if (status_ok())
  168.             return 1;
  169.     } while (--retry);
  170.     return 0;
  171. }
  172.  
  173. static int status_ok(void)
  174. {
  175.     unsigned char status = inb_p(HD_STATUS);
  176.  
  177.     if (status & BUSY_STAT)
  178.         return 1;
  179.     if (status & WRERR_STAT)
  180.         return 0;
  181.     if (!(status & READY_STAT))
  182.         return 0;
  183.     if (!(status & SEEK_STAT))
  184.         return 0;
  185.     return 1;
  186. }
  187.  
  188. static int controller_busy(void)
  189. {
  190.     int retries = 100000;
  191.     unsigned char status;
  192.  
  193.     do {
  194.         status = inb_p(HD_STATUS);
  195.     } while ((status & BUSY_STAT) && --retries);
  196.     return status;
  197. }
  198.  
  199. static void hd_out(unsigned int drive,unsigned int nsect,unsigned int sect,
  200.         unsigned int head,unsigned int cyl,unsigned int cmd,
  201.         void (*intr_addr)(void))
  202. {
  203.     if (drive>1 || head>15)
  204.         panic("Trying to write bad sector");
  205.     if (reset)
  206.         return;
  207.     if (!controller_ready(drive, head)) {
  208.         reset = 1;
  209.         return;
  210.     }
  211.     SET_INTR(intr_addr);
  212.     outb(hd_info[drive].ctl,HD_CMD);
  213.     outb_p(hd_info[drive].wpcom>>2,HD_PRECOMP);
  214.     outb_p(nsect,HD_NSECTOR);
  215.     outb_p(sect,HD_SECTOR);
  216.     outb_p(cyl,HD_LCYL);
  217.     outb_p(cyl>>8,HD_HCYL);
  218.     outb_p(0xA0|(drive<<4)|head,HD_CURRENT);
  219.     outb_p(cmd,HD_COMMAND);
  220. }
  221.  
  222. static int drive_busy(void)
  223. {
  224.     unsigned int i;
  225.     unsigned char c;
  226.  
  227.     for (i = 0; i < 500000 ; i++) {
  228.         c = inb_p(HD_STATUS);
  229.         c &= (BUSY_STAT | READY_STAT | SEEK_STAT);
  230.         if (c == (READY_STAT | SEEK_STAT))
  231.             return 0;
  232.     }
  233.     printk("HD controller times out, status = 0x%02x\n",c);
  234.     return 1;
  235. }
  236.  
  237. static void reset_controller(void)
  238. {
  239.     int    i;
  240.  
  241.     printk("HD-controller reset\n");
  242.     outb(4,HD_CMD);
  243.     for(i = 0; i < 1000; i++) nop();
  244.     outb(hd_info[0].ctl & 0x0f ,HD_CMD);
  245.     if (drive_busy())
  246.         printk("HD-controller still busy\n");
  247.     if ((hd_error = inb(HD_ERROR)) != 1)
  248.         printk("HD-controller reset failed: %02x\n",hd_error);
  249. }
  250.  
  251. static void reset_hd(void)
  252. {
  253.     static int i;
  254.  
  255. repeat:
  256.     if (reset) {
  257.         reset = 0;
  258.         i = -1;
  259.         reset_controller();
  260.     } else if (win_result()) {
  261.         bad_rw_intr();
  262.         if (reset)
  263.             goto repeat;
  264.     }
  265.     i++;
  266.     if (i < NR_HD) {
  267.         hd_out(i,hd_info[i].sect,hd_info[i].sect,hd_info[i].head-1,
  268.             hd_info[i].cyl,WIN_SPECIFY,&reset_hd);
  269.         if (reset)
  270.             goto repeat;
  271.     } else
  272.         do_hd_request();
  273. }
  274.  
  275. /*
  276.  * Ok, don't know what to do with the unexpected interrupts: on some machines
  277.  * doing a reset and a retry seems to result in an eternal loop. Right now I
  278.  * ignore it, and just set the timeout.
  279.  */
  280. void unexpected_hd_interrupt(void)
  281. {
  282.     printk("Unexpected HD interrupt\n");
  283.     SET_TIMER;
  284. }
  285.  
  286. /*
  287.  * bad_rw_intr() now tries to be a bit smarter and does things
  288.  * according to the error returned by the controller.
  289.  * -Mika Liljeberg (liljeber@cs.Helsinki.FI)
  290.  */
  291. static void bad_rw_intr(void)
  292. {
  293.     int dev;
  294.  
  295.     if (!CURRENT)
  296.         return;
  297.     dev = MINOR(CURRENT->dev) >> 6;
  298.     if (++CURRENT->errors >= MAX_ERRORS || (hd_error & BBD_ERR)) {
  299.         end_request(0);
  300.         recalibrate[dev] = 1;
  301.     } else if (CURRENT->errors % RESET_FREQ == 0)
  302.         reset = 1;
  303.     else if ((hd_error & TRK0_ERR) || CURRENT->errors % RECAL_FREQ == 0)
  304.         recalibrate[dev] = 1;
  305.     /* Otherwise just retry */
  306. }
  307.  
  308. static inline int wait_DRQ(void)
  309. {
  310.     int retries = 100000;
  311.  
  312.     while (--retries > 0)
  313.         if (inb_p(HD_STATUS) & DRQ_STAT)
  314.             return 0;
  315.     return -1;
  316. }
  317.  
  318. #define STAT_MASK (BUSY_STAT | READY_STAT | WRERR_STAT | SEEK_STAT | ERR_STAT)
  319. #define STAT_OK (READY_STAT | SEEK_STAT)
  320.  
  321. static void read_intr(void)
  322. {
  323.     int i;
  324.     int retries = 100000;
  325.  
  326.     do {
  327.         i = (unsigned) inb_p(HD_STATUS);
  328.         if (i & BUSY_STAT)
  329.             continue;
  330.         if ((i & STAT_MASK) != STAT_OK)
  331.             break;
  332.         if (i & DRQ_STAT)
  333.             goto ok_to_read;
  334.     } while (--retries > 0);
  335.     printk("HD: read_intr: status = 0x%02x\n",i);
  336.     if (i & ERR_STAT) {
  337.         hd_error = (unsigned) inb(HD_ERROR);
  338.         printk("HD: read_intr: error = 0x%02x\n",hd_error);
  339.     }
  340.     bad_rw_intr();
  341.     do_hd_request();
  342.     return;
  343. ok_to_read:
  344.     port_read(HD_DATA,CURRENT->buffer,15);
  345.     CURRENT->errors = 0;
  346.     CURRENT->buffer += 512;
  347.     CURRENT->sector++;
  348.     i = --CURRENT->nr_sectors;
  349.     --CURRENT->current_nr_sectors;
  350. #ifdef DEBUG
  351.     printk("hd%d : sector = %d, %d remaining to buffer = %08x\n",
  352.         MINOR(CURRENT->dev), CURRENT->sector, i, CURRENT-> 
  353.         buffer);
  354. #endif
  355.     if (!i || (CURRENT->bh && !SUBSECTOR(i)))
  356.         end_request(1);
  357.     if (i > 0) {
  358.         SET_INTR(&read_intr);
  359.         return;
  360.     }
  361.     (void) inb_p(HD_STATUS);
  362. #if (HD_DELAY > 0)
  363.     last_req = read_timer();
  364. #endif
  365.     do_hd_request();
  366.     return;
  367. }
  368.  
  369. static void write_intr(void)
  370. {
  371.     int i;
  372.     int retries = 100000;
  373.  
  374.     do {
  375.         i = (unsigned) inb_p(HD_STATUS);
  376.         if (i & BUSY_STAT)
  377.             continue;
  378.         if ((i & STAT_MASK) != STAT_OK)
  379.             break;
  380.         if ((CURRENT->nr_sectors <= 1) || (i & DRQ_STAT))
  381.             goto ok_to_write;
  382.     } while (--retries > 0);
  383.     printk("HD: write_intr: status = 0x%02x\n",i);
  384.     if (i & ERR_STAT) {
  385.         hd_error = (unsigned) inb(HD_ERROR);
  386.         printk("HD: write_intr: error = 0x%02x\n",hd_error);
  387.     }
  388.     bad_rw_intr();
  389.     do_hd_request();
  390.     return;
  391. ok_to_write:
  392.     CURRENT->sector++;
  393.     i = --CURRENT->nr_sectors;
  394.     --CURRENT->current_nr_sectors;
  395.     CURRENT->buffer += 512;
  396.     if (!i || (CURRENT->bh && !SUBSECTOR(i)))
  397.         end_request(1);
  398.     if (i > 0) {
  399.       SET_INTR(&write_intr);
  400.         port_write(HD_DATA,CURRENT->buffer,15);
  401.     } else {
  402.         do_hd_request();
  403.     }
  404.     return;
  405. }
  406.  
  407. static void recal_intr(void)
  408. {
  409.     if (win_result())
  410.         bad_rw_intr();
  411.     do_hd_request();
  412. }
  413.  
  414. /*
  415.  * This is another of the error-routines I don't know what to do with. The
  416.  * best idea seems to just set reset, and start all over again.
  417.  */
  418. static void hd_times_out(void)
  419. {
  420.     DEVICE_INTR = NULL;
  421.     reset = 1;
  422.     if (!CURRENT)
  423.         return;
  424.     printk("HD timeout\n");
  425.     if (++CURRENT->errors >= MAX_ERRORS) {
  426. #ifdef DEBUG
  427.         printk("hd : too many errors.\n");
  428. #endif
  429.         end_request(0);
  430.     }
  431.  
  432.     do_hd_request();
  433. }
  434.  
  435. /*
  436.  * The driver has been modified to enable interrupts a bit more: in order to
  437.  * do this we first (a) disable the timeout-interrupt and (b) clear the
  438.  * device-interrupt. This way the interrupts won't mess with out code (the
  439.  * worst that can happen is that an unexpected HD-interrupt comes in and
  440.  * sets the "reset" variable and starts the timer)
  441.  */
  442. static void do_hd_request(void)
  443. {
  444.     unsigned int block,dev;
  445.     unsigned int sec,head,cyl,track;
  446.     unsigned int nsect;
  447.  
  448.     if (CURRENT && CURRENT->dev < 0) return;
  449.  
  450.     if (DEVICE_INTR)
  451.         return;
  452. repeat:
  453.     timer_active &= ~(1<<HD_TIMER);
  454.     INIT_REQUEST;
  455.     dev = MINOR(CURRENT->dev);
  456.     block = CURRENT->sector;
  457.     nsect = CURRENT->nr_sectors;
  458.     if (dev >= (NR_HD<<6) || block >= hd[dev].nr_sects) {
  459. #ifdef DEBUG
  460.         printk("hd%d : attempted read for sector %d past end of device at sector %d.\n",
  461.                block, hd[dev].nr_sects);
  462. #endif
  463.         end_request(0);
  464.         goto repeat;
  465.     }
  466.     block += hd[dev].start_sect;
  467.     dev >>= 6;
  468.     sec = block % hd_info[dev].sect + 1;
  469.     track = block / hd_info[dev].sect;
  470.     head = track % hd_info[dev].head;
  471.     cyl = track / hd_info[dev].head;
  472. #ifdef DEBUG
  473.     printk("hd%d : cyl = %d, head = %d, sector = %d, buffer = %08x\n",
  474.         dev, cyl, head, sec, CURRENT->buffer);
  475. #endif
  476.     if (reset) {
  477.         int i;
  478.  
  479.         for (i=0; i < NR_HD; i++)
  480.             recalibrate[i] = 1;
  481.         reset_hd();
  482.         return;
  483.     }
  484.     if (recalibrate[dev]) {
  485.         recalibrate[dev] = 0;
  486.         hd_out(dev,hd_info[dev].sect,0,0,0,WIN_RESTORE,&recal_intr);
  487.         if (reset)
  488.             goto repeat;
  489.         return;
  490.     }    
  491.     if (CURRENT->cmd == WRITE) {
  492.         hd_out(dev,nsect,sec,head,cyl,WIN_WRITE,&write_intr);
  493.         if (reset)
  494.             goto repeat;
  495.         if (wait_DRQ()) {
  496.             printk("HD: do_hd_request: no DRQ\n");
  497.             bad_rw_intr();
  498.             goto repeat;
  499.         }
  500.         port_write(HD_DATA,CURRENT->buffer,15);
  501.         return;
  502.     }
  503.     if (CURRENT->cmd == READ) {
  504.         hd_out(dev,nsect,sec,head,cyl,WIN_READ,&read_intr);
  505.         if (reset)
  506.             goto repeat;
  507.         return;
  508.     }
  509.     panic("unknown hd-command");
  510. }
  511.  
  512. static int hd_ioctl(struct inode * inode, struct file * file,
  513.     unsigned int cmd, unsigned long arg)
  514. {
  515.     struct hd_geometry *loc = (struct hd_geometry *) arg;
  516.     int dev, err;
  517.  
  518.     if (!inode)
  519.         return -EINVAL;
  520.     dev = MINOR(inode->i_rdev) >> 6;
  521.     if (dev >= NR_HD)
  522.         return -EINVAL;
  523.     switch (cmd) {
  524.         case HDIO_GETGEO:
  525.             if (!loc)  return -EINVAL;
  526.             err = verify_area(VERIFY_WRITE, loc, sizeof(*loc));
  527.             if (err)
  528.                 return err;
  529.             put_fs_byte(hd_info[dev].head,
  530.                 (char *) &loc->heads);
  531.             put_fs_byte(hd_info[dev].sect,
  532.                 (char *) &loc->sectors);
  533.             put_fs_word(hd_info[dev].cyl,
  534.                 (short *) &loc->cylinders);
  535.             put_fs_long(hd[MINOR(inode->i_rdev)].start_sect,
  536.                 (long *) &loc->start);
  537.             return 0;
  538.              case BLKGETSIZE:   /* Return device size */
  539.             if (!arg)  return -EINVAL;
  540.             err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
  541.             if (err)
  542.                 return err;
  543.             put_fs_long(hd[MINOR(inode->i_rdev)].nr_sects,
  544.                 (long *) arg);
  545.             return 0;
  546.         case BLKFLSBUF:
  547.             if(!suser())  return -EACCES;
  548.             if(!inode->i_rdev) return -EINVAL;
  549.             fsync_dev(inode->i_rdev);
  550.             invalidate_buffers(inode->i_rdev);
  551.             return 0;
  552.  
  553.         case BLKRRPART: /* Re-read partition tables */
  554.             return revalidate_hddisk(inode->i_rdev, 1);
  555.         RO_IOCTLS(inode->i_rdev,arg);
  556.         default:
  557.             return -EINVAL;
  558.     }
  559. }
  560.  
  561. static int hd_open(struct inode * inode, struct file * filp)
  562. {
  563.     int target;
  564.     target =  DEVICE_NR(MINOR(inode->i_rdev));
  565.  
  566.     while (busy[target])
  567.         sleep_on(&busy_wait);
  568.     access_count[target]++;
  569.     return 0;
  570. }
  571.  
  572. /*
  573.  * Releasing a block device means we sync() it, so that it can safely
  574.  * be forgotten about...
  575.  */
  576. static void hd_release(struct inode * inode, struct file * file)
  577. {
  578.         int target;
  579.     sync_dev(inode->i_rdev);
  580.  
  581.     target =  DEVICE_NR(MINOR(inode->i_rdev));
  582.     access_count[target]--;
  583.  
  584. }
  585.  
  586. static void hd_geninit(void);
  587.  
  588. static struct gendisk hd_gendisk = {
  589.     MAJOR_NR,    /* Major number */    
  590.     "hd",        /* Major name */
  591.     6,        /* Bits to shift to get real from partition */
  592.     1 << 6,        /* Number of partitions per real */
  593.     MAX_HD,        /* maximum number of real */
  594.     hd_geninit,    /* init function */
  595.     hd,        /* hd struct */
  596.     hd_sizes,    /* block sizes */
  597.     0,        /* number */
  598.     (void *) hd_info,    /* internal */
  599.     NULL        /* next */
  600. };
  601.     
  602. /*te******************************************************************
  603.  *
  604.  * hd_interrupt routine
  605.  *   Test, if this is a harddisk interrupt and call
  606.  *   the irq handler
  607.  *   Otherwise ignore this interrupt.
  608.  *
  609.  ********************************************************/
  610. static void hd_interrupt (struct intframe *fp, void *data)
  611. {
  612. void (*hd_irq_handler)(void) = DEVICE_INTR;
  613.  
  614. /* Test, if this is a harddisk interrupt. MSB(0xdd2030) = 1 */
  615. if (inb (HD_IRQ_TEST) & 0x7f)
  616.   {
  617.   DEVICE_INTR = NULL;
  618.   timer_active &= ~(1 << HD_TIMER);
  619.  
  620.   if (!hd_irq_handler)
  621.     hd_irq_handler = unexpected_hd_interrupt;
  622.   hd_irq_handler ();
  623.  
  624.   return;
  625.   } 
  626. }
  627.  
  628. /*te******************************************************************
  629.  *
  630.  * hd_geninit 
  631.  *   Scan and initialize all connected harddisks.
  632.  *
  633.  ***************************/
  634. static void hd_geninit (void)
  635. {
  636. int i;
  637. int drive;
  638. unsigned short Identify [256];
  639. unsigned char drive_found;
  640.  
  641. printk ("Probing harddisk(s):\n");
  642. NR_HD = 0;
  643.  
  644. outb (IDE_DISABLE_IRQ, HD_CMD);     /* Disable HD-interrupt */
  645. cli ();                             /* Disable interrupts   */
  646.  
  647. for (drive=0; drive<2; drive++)
  648.   {
  649.   if (drive_busy ())
  650.     {
  651.     printk ("hd: Can't access harddisk controller.\n");
  652.     NR_HD = 0;
  653.     break;
  654.     }
  655.  
  656.   /* select drive */
  657.   outb_p ((unsigned char) (0xA0 | (drive<<4)), HD_CURRENT);
  658.  
  659.   /* Set identify command */
  660.   outb_p (WIN_IDENTIFY, HD_COMMAND);
  661.  
  662.   drive_found = 0;
  663.   /* Test, if ready to read */
  664.   for (i=0; i<HD_ID_RETRIES; i++)
  665.     {
  666.     if (inb_p (HD_STATUS) & DRQ_STAT)
  667.       {
  668.       drive_found = 1;
  669.       break;
  670.       }
  671.     }
  672.   
  673.   if (drive_found)
  674.     {
  675.     port_read (HD_DATA, Identify, 15);
  676.  
  677.     hd_info[drive].cyl    = Identify [1];
  678.     hd_info[drive].head   = Identify [3];
  679.     hd_info[drive].wpcom  = 0;
  680.     hd_info[drive].ctl    = 0;
  681.     hd_info[drive].lzone  = Identify [1];
  682.     hd_info[drive].sect   = Identify [6];
  683.     NR_HD++;
  684.     Identify [47] = 0x0000;
  685.     printk ("hd%d: %s\n", drive, (unsigned char *)&Identify [27]);
  686.     printk ("     cyl %d, sector %d, head %d.\n",
  687.                                              hd_info[drive].cyl,
  688.                                              hd_info[drive].sect,
  689.                                              hd_info[drive].head);
  690.     }
  691.   else
  692.     printk ("hd%d not found.\n", drive);
  693.   }
  694. sti ();                             /* Enable interrupts */
  695.  
  696. /* Add the interrupt handler */
  697. if (NR_HD)
  698.   if (!add_isr (IRQ_MFP_FDC, hd_interrupt, 0, NULL))
  699.     {
  700.     printk ("HD: Can't add interrupt handler for harddisk device.\n");
  701.     NR_HD = 0;
  702.     }
  703.     mfp.int_en_b |= 0x80;    /* enable int */
  704.     mfp.int_mk_b |= 0x80;    /* not masked */
  705.  
  706. i = NR_HD;
  707. while (i-- > 0)
  708.     hd[i<<6].nr_sects = hd_info[i].head * hd_info[i].sect
  709.                       * hd_info[i].cyl;
  710.  
  711. hd_gendisk.nr_real = NR_HD;
  712.  
  713. for (i=0; i<(MAX_HD << 6); i++)
  714.   hd_blocksizes[i] = 1024;
  715.  
  716. blksize_size [MAJOR_NR] = hd_blocksizes;
  717. }
  718.  
  719. static struct file_operations hd_fops = {
  720.     NULL,            /* lseek - default */
  721.     block_read,        /* read - general block-dev read */
  722.     block_write,        /* write - general block-dev write */
  723.     NULL,            /* readdir - bad */
  724.     NULL,            /* select */
  725.     hd_ioctl,        /* ioctl */
  726.     NULL,            /* mmap */
  727.     hd_open,        /* open */
  728.     hd_release,        /* release */
  729.     block_fsync        /* fsync */
  730. };
  731.  
  732. unsigned long hd_init(unsigned long mem_start, unsigned long mem_end)
  733. {
  734.   if (boot_info.machtype!=MACH_ATARI || boot_info.bi_atari.model!=ATARI_FALCON)
  735.       return mem_start;
  736.     if (register_blkdev(MAJOR_NR,"hd",&hd_fops)) {
  737.         printk("Unable to get major %d for harddisk\n",MAJOR_NR);
  738.         return mem_start;
  739.     }
  740.     blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
  741.     read_ahead[MAJOR_NR] = 8;        /* 8 sector (4kB) read-ahead */
  742.     hd_gendisk.next = gendisk_head;
  743.     gendisk_head = &hd_gendisk;
  744.     timer_table[HD_TIMER].fn = hd_times_out;
  745.     return mem_start;
  746. }
  747.  
  748. #define DEVICE_BUSY busy[target]
  749. #define USAGE access_count[target]
  750. #define CAPACITY (hd_info[target].head*hd_info[target].sect*hd_info[target].cyl)
  751. /* We assume that the the bios parameters do not change, so the disk capacity
  752.    will not change */
  753. #undef MAYBE_REINIT
  754. #define GENDISK_STRUCT hd_gendisk
  755.  
  756. /*
  757.  * This routine is called to flush all partitions and partition tables
  758.  * for a changed scsi disk, and then re-read the new partition table.
  759.  * If we are revalidating a disk because of a media change, then we
  760.  * enter with usage == 0.  If we are using an ioctl, we automatically have
  761.  * usage == 1 (we need an open channel to use an ioctl :-), so this
  762.  * is our limit.
  763.  */
  764. static int revalidate_hddisk(int dev, int maxusage)
  765. {
  766.     int target, major;
  767.     struct gendisk * gdev;
  768.     int max_p;
  769.     int start;
  770.     int i;
  771.  
  772.     target =  DEVICE_NR(MINOR(dev));
  773.     gdev = &GENDISK_STRUCT;
  774.  
  775.     cli();
  776.     if (DEVICE_BUSY || USAGE > maxusage) {
  777.         sti();
  778.         return -EBUSY;
  779.     };
  780.     DEVICE_BUSY = 1;
  781.     sti();
  782.  
  783.     max_p = gdev->max_p;
  784.     start = target << gdev->minor_shift;
  785.     major = MAJOR_NR << 8;
  786.  
  787.     for (i=max_p - 1; i >=0 ; i--) {
  788.         sync_dev(major | start | i);
  789.         invalidate_inodes(major | start | i);
  790.         invalidate_buffers(major | start | i);
  791.         gdev->part[start+i].start_sect = 0;
  792.         gdev->part[start+i].nr_sects = 0;
  793.     };
  794.  
  795. #ifdef MAYBE_REINIT
  796.     MAYBE_REINIT;
  797. #endif
  798.  
  799.     gdev->part[start].nr_sects = CAPACITY;
  800.     resetup_one_dev(gdev, target);
  801.  
  802.     DEVICE_BUSY = 0;
  803.     wake_up(&busy_wait);
  804.     return 0;
  805. }
  806.